//预览按钮
$("#table-search").bind("click", function () {
    window.open('/pInvoices/view', 'newwindow', 'width=' + window.screen.availWidth + ',height=' + window.screen.availHeight + ',top=0,left=0,toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no')
});

//关闭打印的窗口
$("#close").click(function () {
    //如果浏览器是火狐或者谷歌，只能设置为空白，否则可关闭
    if (navigator.userAgent.indexOf("Chrome") > 0 || navigator.userAgent.indexOf("Firefox") > 0) {
        var opened = window.open('about:blank', '_self');
        opened.opener = null;
        opened.close();
    } else {
        window.opener = null;
        window.open('', '_self');
        window.close();
    }
});

//打印按钮事件
$("#table-add").click(function () {
    $(this).attr('disabled','disabled').die('click');
    var invoicePrintType;//打印方式
   // var invoicePrintMode;//打印设置框弹出
    if(typeof $('input:radio[name="printType"]:checked').val() == "undefined"){
        alert("请选择打印类型!");
        return;
    }
    // else if(typeof $('input:radio[name="printWay"]:checked').val() == "undefined"){
    //     invoicePrintMode = "1";//默认仅弹出一次
    //     invoicePrintType = $('input:radio[name="printType"]:checked').val();//同时设置选择的打印方式
    // }
    else{
        //invoicePrintMode = $('input:radio[name="printWay"]:checked').val();//设置选择的弹出方式
        invoicePrintType = $('input:radio[name="printType"]:checked').val();//同时设置选择的打印方式
    }


    //获取发票类型和发票记录id
    var blueRedType = $("#invoiceType").html();
    var invoiceGuid = $("#invoiceGuid").html();
    if("0"==blueRedType){
        blueRedType = "blue";
    }else if("1"==blueRedType){
        blueRedType = "red";
    }
    console.log("blueRedType:"+blueRedType+";invoiceGuid:"+invoiceGuid+";invoicePrintType:"+invoicePrintType);
    $.ajax({
        url: "/invoice/print",
        type: "post",
        datatype: "json",
        data: JSON.stringify({
            "blueRedType":blueRedType,
            "invoiceGuid":invoiceGuid,
            "invoicePrintType":invoicePrintType
        }),
        contentType: "application/json",
        success: function (data) {
            console.log(data.errorCode);
            console.log(data.errorMessage);
            if('0000' == data.errorCode){
                alert("打印成功！");
                window.location.reload(true);
            }
            else{
                alert(data.errorMessage);
            }
        },
        error: function (data) {
            alert("打印出错！");
        }
    })

});


//模糊搜索
//--------------------------------模糊搜索项目begin--------------------------------------
//实现搜索功能
/**
 * 搜索下拉列表
 **/
$("#testInput").bind("input propertychange", function () {
    inputChange();
});
//1：搜索数据
var inputChange = function () {
    var inputValue = $("#testInput").val();

    if (inputValue != "") {
        $.ajax({
            url: '/invoice/getMessageByName',
            type: 'get',
            async: true,
            data: {"name": inputValue},
            dataType: 'json',
            contentType: 'application/json;charse=UTF-8',
            success: function (data) {
                console.log(data);
                if (data != undefined) {
                    _initItems(data);
                }
            },
            error: function () {
                alert("操作失败");
            }
        })
    }
};
var maxFontNumber = 0;//最大字数
var suggestContainer = $('<div></div>'); //创建一个子<ul>
suggestContainer.attr('id', "testInput-suggest");
suggestContainer.attr('tabindex', '0');
suggestContainer.hide();

//2：初始化搜索到的数据进行显示
var _initItems = function (items) {
    $('#ruleOrgCode').val("");
    suggestContainer.empty();
    for (var i = 0; i < items.length; i++) {
        if (items[i].Name.length > maxFontNumber) {
            maxFontNumber = items[i].Name.length;
        }
        var suggestItem = $('<div></div>'); //创建一个子<li>
        suggestItem.attr('code', items[i].Id);
        suggestItem.append(items[i].Name);
        suggestItem.css({
            'width': '100%',
            'padding': '10px',//item间距
            'word-wrap': 'break-word',
            'word-break': 'normal',
            'cursor': 'pointer',
            'background-color': '#eeeeee',//默认背景颜色
            'color': '#000',//默认字体颜色
            'text-align': 'left',
            'border-bottom': '1px solid rgb(204,204,204)'
        });
        suggestItem.bind("mouseover",
            function () {
                $(this).css({
                    'background-color': '#ffffff',//选中背景颜色
                    'color': '#000000'//选中字体颜色
                });
            });
        suggestItem.bind("mouseout",
            function () {
                $(this).css({
                    'background-color': '#eeeeee',//默认背景颜色
                    'color': '#000000'//默认字体颜色
                });
            });
        suggestItem.bind("click", showClickTextFunction());//选中后处理数据
        //suggestItem.bind("click", itemSelectFunction);
        suggestItem.appendTo(suggestContainer);
        suggestContainer.appendTo(document.body);
    }
    suggestContainer.removeAttr("style");
    suggestContainer.css({
        'border': '1px solid #ccc',
        'max-height': '200px',
        'top': $('#testInput').offset().top + $('#testInput').outerHeight(),
        'left': $('#testInput').offset().left,
        'width': $('#testInput').parents().width(),//获取项目的宽度加上边框边距
        'position': 'absolute',
        'font-size': '12px',//默认字体大小
        'font-family': 'Arial',
        'z-index': 99999,
        'background-color': '#EEE',
        'overflow-y': 'auto',
        'overflow-x': 'hidden',
        'paddding-left': '0!important',
        'margin-left': '0!important'

    });
    maxFontNumber = 0;
    suggestContainer.show();
};

//3.选中后处理数据
var showClickTextFunction = function () {
    //alert(this.innerText + "---" + this.getAttribute("Id"));
    $('#testInput').val(this.innerText);
    $('#ruleOrgCode').val(this.getAttribute("Id"));
//        document.getElementById('thisPage:thisform:proId').value = $(this).attr('code');    ///选中项目的ID
    suggestContainer.hide();
};

//4.点击空白地方 隐藏
$(document).bind("click", function (e) {
    var target = $(e.target);
    if (target.closest(".pop").length == 0) {
        suggestContainer.hide();
    }
})
//-------------------------模糊搜索项目end------------------------------------------------